const { Router } = require("express");
const Recipe = require("../model/Recipe");
const User = require("../model/User");
const Collect = require("../model/Collect")

const router = new Router();
router.use(require('./auths.js'));

// 增加收藏夹
/* 
url:localhost:8000/api/Collect/addCollect
method:POST
header: authorization (user_id：用户id)
body:collect_name
*/
router.post('/addCollect', async (req, res) => {
    // 获取参数
    const user_id = req.userInfo._id;
    const { collect_name } = req.body
    let a = await Collect.find({ user_id, collect_name })
    if (a.length == 0) {
        // 增加收藏夹
        let data = await Collect.create({ user_id, collect_name })
        res.json({ message: "成功创建", data });
        return
    }
    res.status(200).json({ message: "收藏夹已存在", });
})

// 查询用户所有收藏夹
router.get('/findCollect', async (req, res) => {
    // 获取参数
    const user_id = req.userInfo._id;
    let data = await Collect.find({ user_id })
    res.status(200).json({ message: "ok", data });
})

// 查询收藏夹里面的菜谱
router.get('/findCollectList', async (req, res) => {
    // 获取参数
    const user_id = req.userInfo._id;
    const { collect_name } = req.query

    const data = await Recipe.find({ user_id, collect_name }).populate(['recipe_id']);
    console.log(data);
    res.status(200).json({ message: "ok", data });
})


// 把菜谱收藏到收藏夹里
router.post('/putIntoCollect', async (req, res) => {
    console.log(111);
    // 获取参数
    const user_id = req.userInfo._id;
    const { recipe_id, collect_name } = req.body
    let data = await Collect.create({ user_id, recipe_id, collect_name })
    res.status(200).json({ message: "收藏夹成功", data });
})

module.exports = router;


